$(document).ready(function() {

    //var log= console.log;

    if (method == 'details' || method == 'details_preview') {
        // Lightbox
        $('a.lightbox').lightBox({
            overlayBgColor: '#000',
            overlayOpacity: 0.6,
            imageLoading: base_url + 'assets/img/lightbox/loading.gif',
            imageBtnClose: base_url + 'assets/img/lightbox/close.gif',
            imageBtnPrev: base_url + 'assets/img/lightbox/prev.gif',
            imageBtnNext: base_url + 'assets/img/lightbox/next.gif',
            imageBlank: base_url + 'assets/img/lightbox/blank.gif',
            containerResizeSpeed: 350,
            txtImage: 'Image',
            txtOf: 'of'
        });
    }

    if (method != 'checkout') {
        // Custom combox
        $('.dropdownized').dropdownized();
    }

    // Search excursions
    $('#changeDetasilsButton').live('click', function() {

        $('#changeDetasilsBox').hide();
        $('#serchModule').show();
        // Custom combox
        //$('.dropdownized').dropdownized();
        return false;

    })

    /***********************************************************
     * Search submit with fake form
     ***********************************************************/
    $('.submit_search').live('click', function() {
        var url = jQuery(this).attr('href');
        $('#submit_search').attr('action', url);
        $('#submit_search').submit();
        return false;
    });




    /***********************************************************
     * JQUERY UI Calendar with event
     ***********************************************************/
    init_dates = function() {

        //console.log('init');

        var date = new Date();
        var start = "#check_in";
        var end = "#check_out";

        //  limiter 1 - today is not available
        //  limiter 0 - today is available
        var limiter = 1;

        var m = date.getMonth(),
                d = date.getDate() + limiter,
                y = date.getFullYear();

        // Disable all dates till today
        $(start).datepicker({
            minDate: new Date(y, m, d),
            dateFormat: 'dd.mm.yy',
            showOn: 'both',
            buttonImage: base_url + "assets/img/icon-datepicker.png",
            buttonImageOnly: true,
            nextText: '&raquo;',
            prevText: '&laquo;',
            onSelect: function(dateStr) {

                var depart = $.datepicker.parseDate('dd.mm.yy', dateStr);

                $(end).datepicker("option", "minDate", depart);

                depart.setDate(depart.getDate() + limiter);
                $(end).datepicker("setDate", depart);

            }
        });

        $(end).datepicker({
            minDate: new Date(y, m, d),
            dateFormat: 'dd.mm.yy',
            showOn: 'both',
            buttonImage: base_url + "assets/img/icon-datepicker.png",
            buttonImageOnly: true,
            nextText: '&raquo;',
            prevText: '&laquo;',
            onSelect: function(dateStr) {
                //$(hidden_end).val($.datepicker.formatDate('dd.mm.yy', $(end).datepicker('getDate')));
            }
        })

        // SET DATE
        $(start).datepicker('setDate', new Date(y, m, d));
        $(end).datepicker('setDate', new Date(y, m, d + 1));

        // Set custom class
        $("#ui-datepicker-div").addClass("newcalendar");
        $("#ui-datepicker-div").addClass("singleCalendar");


        function set_checkin_checkout() {

            //set pickupdate from session
            var n1 = check_in.split(".");
            $(start).datepicker('setDate', new Date(n1[2], n1[1] - 1, n1[0]));

            // set mindate for checkout
            $(end).datepicker("option", "minDate", new Date(n1[2], n1[1] - 1, parseInt(n1[0]) + 1));

            //set returndate from session
            var n2 = check_out.split(".");
            $(end).datepicker('setDate', new Date(n2[2], n2[1] - 1, n2[0]));
        }

        // METHOD IS SEARCH - set dates from $session variable
        if (method == "search" || method == "details") {

            set_checkin_checkout();

        }

        if (typeof is_post_room_search === 'undefined') {

        } else {

            set_checkin_checkout();

        }

    }

    init_dates();

    $("#dest_hot").autocomplete({
        source: function(request, response) {
            $.ajax({
                url: base_url + lang + "/hotels/destinations_hotels_JSON",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 9,
                    name_startsWith: request.term
                },
                success: function(data) {
                    response($.map(data.items, function(item) {

                        return {
                            label: item.name + (item.parent ? ", " + item.parent : ""),
                            value: item.name,
                            category: item.category
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function(event, ui) {
        },
        open: function() {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function() {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });



    // Childs
    do_childs = function(nr) {
        nr = parseInt(nr);
        //console.log(nr)
        if (nr == 0) {
            $(".ages").hide();
        } else {
            $(".ages").show();

            for (i = 1; i <= nr; i++) {
                $(".age-" + i).show();
            }
            for (i = nr + 1; i <= 5; i++) {
                $(".age-" + i + " input").val("");
                $(".age-" + i).hide();
            }
        }
    }



    // Do children
    $(".positive-integer").numeric({
        decimal: false,
        negative: false
    }, function() {
        alert("Positive integers only");
        this.value = "";
        this.focus();
    });

    $("#nr_childrens").live("change", function() {
        do_childs($(this).val());
    })

    // do validation
    $('#hotelsSearch .button-search').live("click", function() {

        // validate destination/hotel
        if ($("#dest_hot").val() == "") {
            //            $("#freeow").freeow("", "Please enter at least part of the name to start searching.", {
            //                classes: ["error"],  
            //                autoHide: true,
            //                autoHideDelay: 5000,
            //                hideDuration: 1500
            //            });
            //            return false; 

            $('#do-search').trigger('click');
        }

        if (!$('#specific_dates').is(':checked')) {
            // do validation for childs age
            nr = $("#nr_childrens").val();
            for (i = 1; i <= nr; i++) {
                if ($(".age-" + i + " input").val() == "") {
                    $("#freeow").freeow("", "Please fill age(s) of children.", {
                        classes: ["error"],
                        autoHide: true,
                        autoHideDelay: 5000,
                        hideDuration: 1500
                    });
                    return false;
                }
            }
        }
        //return false;
    })

    $('#do-search').live('click', function() {
           $('#hotelsSearch').submit();
           return false;
    });

    //Do reset ages
    if (typeof doResetAges === 'undefined') {

    } else {
        if (doResetAges) {
            //console.log(" doResetAges")
            // do resete childs and ages
            $("#nr_childrens").val(0);
            for (i = 1; i <= 5; i++) {
                $(".age-" + i + " input").val("");
            }
        }


        // set visible childs ages
        //console.log(nr_children)
        do_childs(nr_children);


        // specifi dates
        do_ages_disable = function() {
            if ($('#specific_dates').is(':checked')) {
                $(".ages").addClass("disable");
                $(".ages input").attr("disabled", "disabled");
            } else {
                $(".ages").removeClass("disable");
            }
        }

        $('#specific_dates').live("change", function() {
            do_ages_disable();
        })

        do_ages_disable();

    }

    $(".tooltip").tooltip({
        show: {
            delay: 250
        },
        track: true
    });

    $('.search-list .car-box .details p').trunk8({lines: 2});
    $('.car-box h4').trunk8({lines: 2});
});

$(window).load(function() {
    $('.search-list .car-box .details p').trunk8({lines: 2});
    $('.car-box h4').trunk8({lines: 2});
});